package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.GoodsCategory;
import cn.wolfcode.car.business.domain.GoodsCategory;
import cn.wolfcode.car.business.query.GoodsCategoryQuery;
import cn.wolfcode.car.business.service.ICustomerService;
import cn.wolfcode.car.business.service.IGoodsCategoryService;
import cn.wolfcode.car.business.service.ILinkmenService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/goodsCategory")
public class GoodsCategoryController {
    //模板前缀
    private static final String prefix = "business/goodsCategory/";

    @Autowired
    private IGoodsCategoryService goodsCategoryService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:goodsCategory:view")
    @RequestMapping("/listPage")
    public String listPage(Model model){
        model.addAttribute("GoodsCategorys",goodsCategoryService.list());
        return prefix + "list";
    }

    @RequiresPermissions("business:goodsCategory:add")
    @RequestMapping("/addPage")
    public String addPage(Model model){
        model.addAttribute("GoodsCategorys",goodsCategoryService.list());
        return prefix + "add";
    }

    //新增
    @RequiresPermissions("business:goodsCategory:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Long superId, GoodsCategory goodsCategory){
        goodsCategoryService.save(superId,goodsCategory);
        return AjaxResult.success();
    }

    //编辑页面
    @RequiresPermissions("business:goodsCategory:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("GoodsCategorys",goodsCategoryService.list());
        GoodsCategory goodsCategory = goodsCategoryService.get(id);
        model.addAttribute("goodsCategory", goodsCategory);
        //查询出父类,放到作用域
        GoodsCategory supperCategory = goodsCategoryService.get(goodsCategory.getParentId());
        model.addAttribute("supperCategory",supperCategory);
        return prefix + "edit";
    }

    //编辑
    @RequiresPermissions("business:goodsCategory:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(GoodsCategory goodsCategory){
        goodsCategoryService.update(goodsCategory);
        return AjaxResult.success();
    }

    //列表
    @RequiresPermissions("business:goodsCategory:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<GoodsCategory> query(GoodsCategoryQuery qo){
        return goodsCategoryService.query(qo);
    }

    //删除  remove
    @RequiresPermissions("system:goodsCategory:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        goodsCategoryService.deleteCategory(ids);
        return AjaxResult.success();
    }

    //树结构 treeData
    @RequestMapping("/treeData")
    @ResponseBody
    public AjaxResult treeData(){
        List<GoodsCategory> list = goodsCategoryService.list();
        return AjaxResult.success(list);
    }

}
